package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.*;
import com.cheer.entity.MallShopEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.MallAdminService;
import com.cheer.service.MallSalesroomService;
import com.cheer.service.MallShopService;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/shop")
public class MallShopController {

    @Autowired
    private MallShopService mallShopService;

    @Autowired
    private MallAdminService mallAdminService;

    @Autowired
    private MallSalesroomService mallSalesroomService;

    @ApiOperation("查询商户分页列表")
    @PostMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<MallShopListVO>> list(@RequestBody MallShopQueryDTO shopQueryDTO) {
        return Result.success(mallShopService.listByQueryCondition(shopQueryDTO));
    }

    @ApiOperation("添加商户")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    @Transactional
    public Result add(@RequestBody @Valid MallShopAddDTO shopAddDTO) {
        // 创建商户
        mallShopService.add(shopAddDTO);
        // 注册用户
        mallAdminService.addShopAdmin(shopAddDTO.getPhone());
        return Result.success();
    }

    @ApiOperation("查询商户信息")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<MallShopInfoVO> info(@RequestParam Long id) {
        return Result.success(mallShopService.info(id));
    }

    @ApiOperation("修改商户信息")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid MallShopUpdateDTO shopUpdateDTO) {
        MallShopEntity shop = mallShopService.getById(shopUpdateDTO.getId());
        if (shop == null) {
            throw new ServiceException("商户不存在");
        }
        // 修改商户信息
        mallShopService.update(shopUpdateDTO);
        return Result.success();
    }

    @ApiOperation("删除商户")
    @PostMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    @Transactional
    public Result delete(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Result.success();
        }
        // 删除商户账号
        List<String> phoneList = mallShopService.listByIds(ids).stream()
                .map(MallShopEntity::getPhone)
                .collect(Collectors.toList());
        mallAdminService.deleteByUsername(phoneList);
        // 删除商户信息
        mallShopService.delete(ids);
        return Result.success();
    }

    @ApiOperation("重置密码")
    @GetMapping("/reset")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result reset(@RequestParam Long id){
        MallShopEntity shop = mallShopService.getById(id);
        if (shop == null) {
            throw new ServiceException("商户不存在");
        }
        mallAdminService.resetShopPassword(shop.getPhone());
        return Result.success();
    }

    @ApiOperation("查询所有商户简略信息")
    @PostMapping("/list/all")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<SimpleObjectVO>> listAll() {
        return Result.success(mallShopService.listAllSimply());
    }

    @ApiOperation("查询门店分页列表")
    @GetMapping("/list/salesroom")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<MallSalesroomListVO>> listSalesroom(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                           @RequestParam Long shopId) {
        return Result.success(mallSalesroomService.list(pageNum, pageSize, shopId));
    }

    @ApiOperation("添加门店")
    @PostMapping("/add/salesroom")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result addSalesroom(@RequestBody @Valid MallSalesroomAddDTO salesroomAddDTO) {
        mallSalesroomService.add(salesroomAddDTO);
        return Result.success();
    }

    @ApiOperation("查看门店详情")
    @GetMapping("/info/salesroom")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<MallSalesroomInfoVO> infoSalesroom(@RequestParam Long salesroomId) {
        return Result.success(mallSalesroomService.info(salesroomId));
    }

    @ApiOperation("修改门店")
    @PostMapping("/update/salesroom")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateSalesroom(@RequestBody @Valid MallSalesroomUpdateDTO salesroomUpdateDTO) {
        mallSalesroomService.update(salesroomUpdateDTO);
        return Result.success();
    }

    @ApiOperation("删除门店")
    @PostMapping("/delete/salesroom")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result deleteSalesroom(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Result.success();
        }
        mallSalesroomService.removeByIds(ids);
        return Result.success();
    }
}
